<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="5" :sm="24">
              <a-form-item label="物流公司" prop="deptId">
                <a-select v-model="queryParam.deptId" placeholder="请选择物流公司" :filterOption="filterOption" show-search>
                  <a-select-option v-for="(d, index) in deptOptions" :key="index" :value="d.deptId">{{ d.deptName
                    }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="用车任务" prop="vehicleName">
                <a-input v-model="queryParam.vehicleName" placeholder="请输入用车任务" allow-clear/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="5" :sm="24">
                <a-form-item label="车牌号" prop="carNo">
                  <a-input v-model="queryParam.carNo" placeholder="请输入车牌号" allow-clear/>
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="24">
                <a-form-item label="接收时间" prop="acceptTime">
                  <a-range-picker style="width: 100%"  format="YYYY-MM-DD HH:mm:ss" allow-clear @change="handleTime"/>
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="24">
                <a-form-item label="接收人" prop="acceptBy">
                  <a-input v-model="queryParam.acceptBy" placeholder="请输入接收人" allow-clear/>
                </a-form-item>
              </a-col>
<!--              <a-col :md="5" :sm="8">-->
<!--                <a-form-item label="订单状态" prop="status">-->
<!--                  <a-select v-model="queryParam.status" placeholder="请选择订单状态">-->
<!--                    <a-select-option v-for="(d, index) in carStatusList" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>-->
<!--                  </a-select>-->
<!--                </a-form-item>-->
<!--              </a-col>-->
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作 -->
      <div class="table-operations">
        <a-button type="primary" @click="handleExport" v-hasPermi="['system:info:export']">
          <a-icon type="download" />导出
        </a-button>
        <table-setting
          :style="{float: 'right'}"
          :table-size.sync="tableSize"
          v-model="columns"
          :refresh-loading="loading"
          @refresh="getList" />
      </div>
      <!-- 接单弹框 -->
      <create-form
        ref="createForm"
        @ok="getList"
      />
      <!-- 上传文件 -->
      <import-excel
          ref="importExcel"
          @ok="getList"
      />
      <!-- 数据展示 -->
      <a-table
        :loading="loading"
        :size="tableSize"
        rowKey="id"
        :columns="columns"
        :data-source="list"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :pagination="false"
        :bordered="tableBordered"
      >
        <span slot="status" slot-scope="text, record">
          <!-- {{ parseTime(record.acceptTime) }} -->
          {{ dict.type['order_status'].find(item=>item.value == record.status)?.label ?? '' }}
        </span>
        <span slot="acceptTime" slot-scope="text, record">
          {{ parseTime(record.acceptTime) }}
        </span>
        <span slot="pointType" slot-scope="text, record">
          {{ dict.type['point_type'].find(item=>item.value == record.pointType)?.label ?? '' }}
        </span>
        <span slot="applyType" slot-scope="text, record">
          {{ dict.type['apply_type'].find(item=>item.value == record.applyType)?.label ?? '' }}
        </span>
        <span slot="operation" slot-scope="text, record">
          <a v-if="record.pointType == 1" @click="$refs.createForm.handleAdd(record,0)" v-hasPermi="['system:info:edit']">
            <a-icon type="edit" />接单
          </a>
          <a-divider v-if="record.pointType == 1" type="vertical" v-hasPermi="['system:info:remove']" />
          <a v-if="record.pointType ==1"  @click="handleReject(record)" v-hasPermi="['system:info:edit']">
            <a-icon type="edit" />拒绝接单
          </a>
          <a-divider v-if="record.pointType == 1" type="vertical" v-hasPermi="['system:info:edit']" />
          <a @click="$refs.createForm.handleAdd(record,2)" v-hasPermi="['system:info:edit']">
            <a-icon type="edit" />详情
          </a>
        </span>
      </a-table>
      <!-- 分页 -->
      <a-pagination
        class="ant-table-pagination"
        show-size-changer
        show-quick-jumper
        :current="queryParam.pageNum"
        :total="total"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        @showSizeChange="onShowSizeChange"
        @change="changeSize"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { listInfo, delBatchInfo,refuseOrder } from '@/api/vehicle/orderinfo'
import CreateForm from './modules/CreateForm'
import ImportExcel from './modules/ImportExcel'
import { tableMixin } from '@/store/table-mixin'
import { getLogistics } from '@/api/system/user'
import { getDicts } from '@/api/system/dict/data'
export default {
  name: 'Info',
  components: {
    CreateForm,
    ImportExcel,
  },
  dicts: ['order_status','apply_type','point_type'],
  mixins: [tableMixin],
  data () {
    return {
      list: [],
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非多个禁用
      multiple: true,
      ids: [],
      loading: false,
      total: 0,
      // 查询参数
      queryParam: {
        vehicleName: null,
        deptId: undefined,
        carNo: null,
        status: '0',
        pointType: null,
        acceptDateStart: null,
        acceptDateEnd: null,
        acceptBy: null,
        pageNum: 1,
        pageSize: 10
      },
      deptOptions: [],
      carStatusList: [],
      replaceFields: { children: 'children', title: 'label', key: 'id', value: 'id' },
      columns: [
        {
          title: '用车任务',
          dataIndex: 'vehicleName',
          align: 'center'
        },
        {
          title: '车牌号',
          dataIndex: 'carNo',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '司机',
          dataIndex: 'driverName',
          ellipsis: true,
          align: 'center'
        },
        // {
        //   title: '状态',
        //   dataIndex: 'status',
        //   ellipsis: true,
        //   scopedSlots: { customRender: 'status' },
        //   align: 'center'
        // },
        {
          title: '当前节点',
          dataIndex: 'pointType',
          align: 'center',
          scopedSlots: { customRender: 'pointType' },
        },
        {
          title: '当前审批状态',
          dataIndex: 'applyType',
          ellipsis: true,
          align: 'center',
          scopedSlots: { customRender: 'applyType' },
        },
        {
          title: '接收时间',
          dataIndex: 'acceptTime',
          scopedSlots: { customRender: 'acceptTime' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '接收人',
          dataIndex: 'acceptBy',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '物流公司',
          dataIndex: 'deptName',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: '18%',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ]
    }
  },
  filters: {
  },
  created () {
    this.getOptionDic()
    this.getList()
  },
  computed: {
  },
  watch: {
  },
  methods: {
    // 筛选功能
    filterOption(inputValue, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(inputValue.toLowerCase()) >= 0
    },    
    handleTime(_a,b){
      this.queryParam.acceptDateStart  = b[0]
      this.queryParam.acceptDateEnd  = b[1]
    },
    /** 查询用车订单列表 */
    getList () {
      this.loading = true
      listInfo(this.queryParam).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getOptionDic(){
      //获取物流公司信息
      getLogistics().then(response => {
        this.deptOptions = response.data.children
      })
      //获取订单状态
      getDicts('order_status').then(res=>{
        this.carStatusList = res.data
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        vehicleId: undefined,
        deptId: undefined,
        carId: undefined,
        driverId: undefined,
        status: '0',
        pointType: undefined,
        acceptTime: undefined,
        acceptBy: undefined,
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.multiple = !selectedRowKeys.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleReject(data){
      var that = this
      this.$confirm({
        title: '是否确认拒接接单?',
        content: '此操作将拒绝接单',
        onOk () {
          refuseOrder({id:data.id}).then((res)=>{
            that.$message.success('拒接接单成功')
          })
        },
        onCancel () {}
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          that.download('order/info/export', {
            ...that.queryParam
          }, `info_${new Date().getTime()}.xlsx`)
        },
        onCancel () {}
      })
    }
  }
}
</script>
